package org.xfy.ymcc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.xfy.common.constant.BusinessKey;
import org.xfy.common.constant.RConst;
import org.xfy.common.result.JsonResult;
import org.xfy.common.util.AssertUtil;
import org.xfy.ymcc.domain.Login;
import org.xfy.ymcc.domain.User;
import org.xfy.ymcc.domain.UserBaseInfo;
import org.xfy.ymcc.domain.UserRealInfo;
import org.xfy.ymcc.dto.UserRegisterDto;
import org.xfy.ymcc.feign.LoginFeignClient;
import org.xfy.ymcc.mapper.UserBaseInfoMapper;
import org.xfy.ymcc.mapper.UserMapper;
import org.xfy.ymcc.mapper.UserRealInfoMapper;
import org.xfy.ymcc.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author xfy
 * @since 2023-11-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginFeignClient loginFeignClient;
    @Autowired
    private UserBaseInfoMapper userBaseInfoMapper;
    @Autowired
    private UserRealInfoMapper userRealInfoMapper;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public JsonResult register(UserRegisterDto userRegisterDto) {
       String phoneKey= userRegisterDto.getMobile()+ BusinessKey.SEGMENTATION+BusinessKey.REGISTER_SMS_CODE_SUFFIX;
        AssertUtil.isTrue(redisTemplate.hasKey(phoneKey),"手机验证码已过期");
        String phoneCode = redisTemplate.opsForValue().get(phoneKey).toString();
        AssertUtil.isTrue(userRegisterDto.getSmsCode().equals(phoneCode),"手机验证码不匹配");
        //查询数据库，手机号是否存在
        User userInfo = userMapper.selectOne
                (new LambdaQueryWrapper<User>().eq(User::getPhone, userRegisterDto.getMobile()));
        AssertUtil.isNull(userInfo,"该手机号已被注册");
        //保存Login信息
        Login login = new Login();
        login.setPassword(new BCryptPasswordEncoder().encode(userRegisterDto.getPassword()));
        login.setUsername(userRegisterDto.getMobile());
        login.setType(userRegisterDto.getRegChannel());
        login.setEnabled(RConst.Num.INT_1);
        JsonResult jsonResult = loginFeignClient.saveOrUpdate(login);
        //openfegin调用 会将Long类型转换为Integer
        Object id = jsonResult.getData();

        //int i=1/0;
        //保存user注册信息
        User user = new User();
        user.setPhone(userRegisterDto.getMobile());
        user.setNickName(userRegisterDto.getMobile());
        user.setLoginId(-1L);
        user.setCreateTime(new Date());
        user.setLoginId(((Integer)id).longValue());
        userMapper.insert(user);

        //保存user_base_info基本信息
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        userBaseInfo.setUserId(user.getId());
        userBaseInfo.setRegChannel(userRegisterDto.getRegChannel());
        userBaseInfoMapper.insert(userBaseInfo);

        //保存t_user_real_info实名认证信息
        UserRealInfo userRealInfo = new UserRealInfo();
        userRealInfo.setUserId(user.getId());
        userRealInfoMapper.insert(userRealInfo);
        return JsonResult.success();
    }
}
